﻿using Common.Exceptions;
using Common.Result;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Extenions
{
    /// <summary>
    /// 通用的结果和异常使用封装
    /// </summary>
    public static class CommonResultExceptionServiceCollectionExtenions
    {
        public static IServiceCollection AddCommonResultException(this IServiceCollection services)
        {
            // 2、配置CommonResultFilter（结果和异常过滤器配置）
            services.Configure<MvcOptions>(options =>
            {
                options.Filters.Add<CommonResultFilter>(); //配置CommonResultFilter
                options.Filters.Add<CommonExceptionFilter>();//配置CommonExceptionFilter
            });

            // 3、配置通用json（json字段信息不做任何改变）
            services.AddControllers().AddJsonOptions(options => {
                options.JsonSerializerOptions.PropertyNamingPolicy = null; // 字段格式不变
            });
            return services;
        }
    }
}
